package com.fezs.common.utils;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.fezs.common.context.BaseContextHandler;
import org.slf4j.MDC;

/**
 * @author zhukai
 * @since 2022/8/18
 */
public class TraceIdUtils {

    /**
     * 塞入跟踪traceId
     *
     * 如果集成了SkyWalking, 可以使用下面获取
     * TraceContext.traceId();
     */
    public static String putTraceId() {
        String traceId = MDC.get(BaseContextHandler.TRACEID);
        if (StrUtil.isBlank(traceId)) {
            traceId = UUID.fastUUID().toString(true);
            MDC.put(BaseContextHandler.TRACEID, traceId);
        }
        return traceId;
    }

    public static void clear() {
        MDC.clear();
    }

}
